1   /*
2    * Copyright (C) 2013 The Guava Authors
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5    * in compliance with the License. You may obtain a copy of the License at
6    *
7    * http://www.apache.org/licenses/LICENSE-2.0
8    *
9    * Unless required by applicable law or agreed to in writing, software distributed under the License
10   * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11   * or implied. See the License for the specific language governing permissions and limitations under
12   * the License.
13   */
14  
15  package com.google.common.collect.testing.google;
16  
17  import static com.google.common.collect.testing.features.CollectionFeature.SUPPORTS_ITERATOR_REMOVE;
18  import static com.google.common.collect.testing.features.CollectionSize.ONE;
19  import static com.google.common.collect.testing.features.CollectionSize.SEVERAL;
20  import static com.google.common.collect.testing.features.MapFeature.ALLOWS_NULL_KEYS;
21  import static com.google.common.collect.testing.features.MapFeature.ALLOWS_NULL_KEY_QUERIES;
22  import static com.google.common.collect.testing.features.MapFeature.SUPPORTS_REMOVE;
23  import static com.google.common.truth.Truth.assertThat;
24  
25  import com.google.common.annotations.GwtCompatible;
26  import com.google.common.collect.Multimap;
27  import com.google.common.collect.Multiset;
28  import com.google.common.collect.Multisets;
29  import com.google.common.collect.testing.Helpers;
30  import com.google.common.collect.testing.features.CollectionFeature;
31  import com.google.common.collect.testing.features.CollectionSize;
32  import com.google.common.collect.testing.features.MapFeature;
33  
34  import java.util.Iterator;
35  
36  /**
37   * Tester for {@code Multimap.entries}.
38   *
39   * @author Louis Wasserman
40   */
41  @GwtCompatible
42  public class MultimapKeysTester<K, V> extends AbstractMultimapTester<K, V, Multimap<K, V>> {
43    @CollectionSize.Require(SEVERAL)
44    public void testKeys() {
45      resetContainer(
46          Helpers.mapEntry(sampleKeys().e0, sampleValues().e0),
47          Helpers.mapEntry(sampleKeys().e0, sampleValues().e1),
48          Helpers.mapEntry(sampleKeys().e1, sampleValues().e0));
49      Multiset<K> keys = multimap().keys();
50      assertEquals(2, keys.count(sampleKeys().e0));
51      assertEquals(1, keys.count(sampleKeys().e1));
52      assertEquals(3, keys.size());
53      assertThat(keys).has().allOf(sampleKeys().e0, sampleKeys().e1);
54      assertThat(keys.entrySet()).has().allOf(
55          Multisets.immutableEntry(sampleKeys().e0, 2),
56          Multisets.immutableEntry(sampleKeys().e1, 1));
57    }
58    
59    @MapFeature.Require(ALLOWS_NULL_KEY_QUERIES)
60    public void testKeysCountAbsentNullKey() {
61      assertEquals(0, multimap().keys().count(null));
62    }
63    
64    @CollectionSize.Require(SEVERAL)
65    @MapFeature.Require(ALLOWS_NULL_KEYS)
66    public void testKeysWithNullKey() {
67      resetContainer(
68          Helpers.mapEntry((K) null, sampleValues().e0),
69          Helpers.mapEntry((K) null, sampleValues().e1),
70          Helpers.mapEntry(sampleKeys().e1, sampleValues().e0));
71      Multiset<K> keys = multimap().keys();
72      assertEquals(2, keys.count(null));
73      assertEquals(1, keys.count(sampleKeys().e1));
74      assertEquals(3, keys.size());
75      assertThat(keys).has().allOf(null, sampleKeys().e1);
76      assertThat(keys.entrySet()).has().allOf(
77          Multisets.immutableEntry((K) null, 2),
78          Multisets.immutableEntry(sampleKeys().e1, 1));
79    }
80    
81    public void testKeysElementSet() {
82      assertEquals(multimap().keySet(), multimap().keys().elementSet());
83    }
84  
85    @MapFeature.Require(SUPPORTS_REMOVE)
86    public void testKeysRemove() {
87      int original = multimap().keys().remove(sampleKeys().e0, 1);
88      assertEquals(Math.max(original - 1, 0), multimap().get(sampleKeys().e0).size());
89    }
90    
91    @CollectionSize.Require(ONE)
92    @CollectionFeature.Require(SUPPORTS_ITERATOR_REMOVE)
93    public void testKeysEntrySetIteratorRemove() {
94      Multiset<K> keys = multimap().keys();
95      Iterator<Multiset.Entry<K>> itr = keys.entrySet().iterator();
96      assertEquals(Multisets.immutableEntry(sampleKeys().e0, 1),
97          itr.next());
98      itr.remove();
99      assertTrue(multimap().isEmpty());
100   }
101   
102   @CollectionSize.Require(SEVERAL)
103   @MapFeature.Require(SUPPORTS_REMOVE)
104   public void testKeysEntrySetRemove() {
105     resetContainer(
106         Helpers.mapEntry(sampleKeys().e0, sampleValues().e0),
107         Helpers.mapEntry(sampleKeys().e0, sampleValues().e1),
108         Helpers.mapEntry(sampleKeys().e1, sampleValues().e0));
109     assertTrue(multimap().keys().entrySet().remove(
110         Multisets.immutableEntry(sampleKeys().e0, 2)));
111     assertEquals(1, multimap().size());
112     assertTrue(multimap().containsEntry(sampleKeys().e1, sampleValues().e0));
113   }
114 }